{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "source": [
    "import tensorflow as tf\r\n",
    "tf.compat.v1.disable_eager_execution()\r\n",
    "m1 = tf.constant([[3,3]])\r\n",
    "m2 = tf.constant([[2,3]])\r\n",
    "dataAdd = tf.add(m1,m2)\r\n",
    "# init = tf.compat.v1.global_variables_initializer()\r\n",
    "with tf.compat.v1.Session() as sess:\r\n",
    "    # sess.run(init)\r\n",
    "    print(sess.run(dataAdd))"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "[[5 6]]\n"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "source": [
    "import tensorflow as tf\r\n",
    "x = tf.Variable([1,2])\r\n",
    "a = tf.constant([3,3])\r\n",
    "sub = tf.subtract(x, a)\r\n",
    "init = tf.compat.v1.global_variables_initializer()\r\n",
    "with tf.compat.v1.Session() as sess:\r\n",
    "    sess.run(init)\r\n",
    "    print(sess.run(sub))"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "[-2 -1]\n"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "source": [
    "import tensorflow as tf\r\n",
    "state = tf.Variable(0,name='counter')\r\n",
    "new_value = tf.add(state,1)\r\n",
    "update =  tf.compat.v1.assign(state,new_value)\r\n",
    "init = tf.compat.v1.global_variables_initializer()\r\n",
    "with tf.compat.v1.Session() as sess:\r\n",
    "    sess.run(init)\r\n",
    "    print(sess.run(state))\r\n",
    "    for _ in range(5):\r\n",
    "        sess.run(update)\r\n",
    "        print(sess.run(state))"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "0\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "source": [
    "#Fetch 同时进行多个op\r\n",
    "import tensorflow as tf\r\n",
    "input1 = tf.constant(3.0)\r\n",
    "input2 = tf.constant(5.0)\r\n",
    "input3 = tf.constant(8.0)\r\n",
    "input4 = tf.constant(9.0)\r\n",
    "\r\n",
    "add = tf.add(input1,input2)\r\n",
    "mul = tf.multiply(input1,add)\r\n",
    "with tf.compat.v1.Session() as sess:\r\n",
    "    result = sess.run([mul,add])\r\n",
    "    print(result)"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "[24.0, 8.0]\n"
     ]
    }
   ],
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "source": [
    "#feed\r\n",
    "input3 = tf.compat.v1.placeholder(tf.float32)\r\n",
    "input4 = tf.compat.v1.placeholder(tf.float32)\r\n",
    "output = tf.multiply(input3,input4)\r\n",
    "with tf.compat.v1.Session() as sess:\r\n",
    "    print(sess.run(output,feed_dict={input3:7.0,input4:10}))"
   ],
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "70.0\n"
     ]
    }
   ],
   "metadata": {}
  }
 ],
 "metadata": {
  "kernelspec": {
   "name": "python3",
   "display_name": "Python 3.8.8 64-bit ('base': conda)"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "interpreter": {
   "hash": "73e03da126b73bfff3642ec5261d56fa25c444ea595de51041687efaa60dda41"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}